package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.atguigu.common.valid.AddGroup;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 属性分组
 *
 * @author humingyang
 * @email humingyang@163.com
 * @date 2021-06-02 23:13:47
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    //获取所有的关联属性下面的关联信息
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntityList = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", attrEntityList);
    }


    /**
     * /product/attrgroup/{attrgroupId}/noattr/relation
     * Author : hmy
     *
     * @date 2021/6/27 18:35
     * desc :查询属性分组没有关联的属性
     **/

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoAttrRelation(@PathVariable("attrgroupId") Long attrgroupId,
                               @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);

        return R.ok().put("data", page);
    }


    /**
     * Author : hmy
     * @date 2021/6/27 17:42
     * desc : 删除属性分组的关联关系
     **/

    @PostMapping("/attr/relation/delete")
    public R getAttrRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos) {

        attrService.deleteRelation(attrGroupRelationVos);


        return R.ok();
    }
    /**
     * Author : hmy
     * @date 2021/6/29 21:13
     * desc : 获取分组下所有的分组，关联关系
     *  /product/attrgroup/{catelogId}/withattr
     **/

    @GetMapping("/{catelogId}/withattr")
    public R getAttrRelation(@PathVariable("catelogId") long catelogId) {

        //查询当前分类下的所有的属性分组
        //每个属性分组的所有属性
      List<AttrGroupWithAttrGroupVo> attrGroupVos =   attrGroupService.getAttrGroupwithattr(catelogId);


        return R.ok().put("data",attrGroupVos);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId) {


//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, categoryId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * Author : hmy
     * @date 2021/6/27 20:44
     * desc :   添加属性与分组的关联关系
     *
     *  /product/attrgroup/attr/relation
     **/

    @PostMapping("/attr/relation")
    //@RequiresPermissions("product:attrgroup:save")
    public R saveRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos) {
        attrAttrgroupRelationService.saveRelations(attrGroupRelationVos);

        return R.ok();
    }




    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
